/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */

package com.yunbiji.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.yunbiji.controller.CalendarController;
import com.yunbiji.controller.EditorController;
import com.yunbiji.controller.LoginController;
import com.yunbiji.controller.MemberFoldersController;
import com.yunbiji.controller.MessageController;
import com.yunbiji.controller.NoteFoldersController;
import com.yunbiji.controller.NotesController;
import com.yunbiji.controller.RegisterController;
import com.yunbiji.controller.UserProfileController;
import com.yunbiji.model.Member_folders;
import com.yunbiji.model.Note_folders;
import com.yunbiji.model.Notes;
import com.yunbiji.model.Users;

/**
 * 该类为配置文件类，用于配置初始化变量
 * @author 聂道鑫
 *
 */
public class NXHZConfig extends JFinalConfig
{
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me)
	{
		// nie:上句话的解释看网址 http://my.oschina.net/skyim/blog/137973
		// nie:getProperty(...)为configPlugin中数据连接池中获得数据
		loadPropertyFile("conf.properties"); // 加载少量必要配置，随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", true));
		me.setFreeMarkerTemplateUpdateDelay(1000);
		me.setBaseViewPath("/WEB-INF/htmls");
		me.setError404View("/WEB-INF/htmls/errors/404.html");
		me.setError500View("/WEB-INF/htmls/errors/500.html");
		me.setMaxPostSize(10 * 1024 * 1024);//设置上传文件大小
	}

	/**
	 * 配置路由 也就是配置路径
	 */
	public void configRoute(Routes me)
	{
		me.add("/", LoginController.class,"/");
		me.add("/bottom",NoteFoldersController.class,"/");
		me.add("/register",RegisterController.class,"/");
		me.add("/notes",NotesController.class,"/");
		me.add("/editor",EditorController.class,"/");
		me.add("/user",UserProfileController.class,"/");
		me.add("/message",MessageController.class,"/");
		me.add("/member", MemberFoldersController.class,"/");
		me.add("/calendar",CalendarController.class,"/");
	}
/**
 * 配置插件，本项目主要配置为数据库的插件配置，数据库连接池使用C3p0连接池
 */
	public void configPlugin(Plugins me)
	{
		/**
		 * 本地数据库连接
		 */
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),getProperty("user"), getProperty("password").trim());
		c3p0Plugin.start();
		// nie: 添加连接池
		me.add(c3p0Plugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		
		
		/**
		 * 连接新浪云的数据配置
		 */
/*		//String user2=SaeUserInfo.getAccessKey();
		//String password2=SaeUserInfo.getSecretKey();
		C3p0Plugin c3p0Plugin2 = new C3p0Plugin(getProperty("jdbcUrl2"),getProperty("user2"), getProperty("password2").trim());
		c3p0Plugin2.start();
		me.add(c3p0Plugin2);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin2);*/
		
		
		// nie: 配置AnsiSqlDialect方言
		arp.setDialect(new AnsiSqlDialect());
		// arp.setShowSql(true);
		// nie: 配置大小写不敏感
		arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
		// nie:接入ActiveRecord插件
		me.add(arp);
		// nie: 将实体和表映射起来
		arp.addMapping("users", Users.class);
		arp.addMapping("note_folders", Note_folders.class);
		arp.addMapping("notes", Notes.class);
		//arp.addMapping("message", Messages.class);2015-5-14删除message功能
		arp.addMapping("member_folders", Member_folders.class);
	
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me)
	{
		// nie: session拦截器，用于在View模板中取出session值
		me.add(new SessionInViewInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me)
	{
		//nie:配置上下文路径
		me.add(new ContextPathHandler("contextPath"));
	}
	/**
	 * 使用框架自带工具进行启动
	 * @param args
	 */
	public static void main(String[] args)
	{
		JFinal.start("WebRoot", 80, "/myybj", 5);
	}
}